<template>
  <el-dialog
    ref="refDlg"
    title="上传配图"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="closeDialog"
    width="700px"
    @close="closeDialog"
  >
    <div v-loading="dataLoading">
      备注：
      <el-input v-model="remark" type="textarea" placeholder="请填写备注" />
      <!-- <import-img
        :photo-list="fileList"
        :config-attachment-id="fileTypeId||''"
      /> -->
      <import-file
        style="padding-top:20px"
        :data="fileList"
        :file-type="fileType"
        :config-attachment-id="fileTypeId||''"
      />
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" :loading="saveLoading" type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import importFile from '@/components/importFile'

import { postConfigAttachmentTemplate, saveUploadPicture } from '@/api/autoDesignTask'

export default {
  name: 'UploadPicDlg',
  components: { importFile },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detail: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      formData: {},
      fileTypeId: '',
      fileType: '',
      remark: '',
      fileList: [],
      dataLoading: false,
      saveLoading: false
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  async created() {
    await postConfigAttachmentTemplate().then(res => {
      if (res.success === 1 && res.data.records.length > 0) {
        // 根据modelId获取每个配置id
        const temp = res.data.records.filter(el => el.enable === 1 && el.attachmentType === '上传配图')
        if (temp.length > 0) {
          this.fileTypeId = temp[0].id
          this.fileType = temp[0].fileType
        }
      }
    })
  },

  methods: {
    async  handleSubmit() {
      this.saveLoading = true
      const data = {
        shopAttachmentDTOList: this.fileList,
        id: this.detail.id,
        remark: this.remark
      }
      console.log('提交表单', data)
      // return
      saveUploadPicture(data).then(res => {
        if (res.success) {
          this.$message.success('上传成功')
          this.closeDialog()
          this.$emit('success')
        }
      })
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {}
    },
    getChangeData() {
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
